package coderead.plugin;

import com.intellij.ide.actions.GotoActionBase;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.*;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.SearchTextField;
import com.intellij.ui.popup.AbstractPopup;
import com.intellij.util.ui.JBInsets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

public class MavenSearchAction extends AnAction {
    public static final DataKey<SearchTextField> KEY = DataKey.create("search.text.field");

    @Override
    public void actionPerformed(AnActionEvent e) {
        openSearch(e);
    }

    public void openSearch(AnActionEvent e){
        String searchText = getInitialTextForNavigation(e.getData(CommonDataKeys.EDITOR));
        openSearch(searchText,e.getProject(),e.getDataContext(),"jar");

    }
    public static void openClassSearch(String searchText, Project project){
        openSearch(searchText,project,DataContext.EMPTY_CONTEXT,"class");
    }

    private static void openSearch(String searchText, Project project, DataContext dataContext,String activeTab){

        SearchPopupFrameUI borderLayoutPanel=new SearchPopupFrameUI(project,searchText,activeTab);
         ComponentPopupBuilder builder =
                JBPopupFactory.getInstance()
                        .createComponentPopupBuilder(borderLayoutPanel, borderLayoutPanel.getSearchField())
//                        .setTitle("class search")
                        .setProject(project)
                        .setModalContext(false)
                        .setCancelOnClickOutside(false)
                        .setRequestFocus(true)
                        .setCancelKeyEnabled(true)
//                        .setCancelOnWindowDeactivation(false)
//                        .setCancelCallback(() -> false)
                        // 鼠标外移回调，TODO 仅在mac 全屏下才启作用
                        .setCancelOnMouseOutCallback(borderLayoutPanel)
                        .addUserData("SIMPLE_WINDOW")
                        .setResizable(true)
                        .setMovable(true)
                        .setDimensionServiceKey(project,KEY.getName(), true)
                        .setLocateWithinScreenBounds(false);
        JBPopup listPopup = builder.createPopup();
        borderLayoutPanel.setJbPopup(listPopup);
        Disposer.register(listPopup, borderLayoutPanel);
        Dimension size = borderLayoutPanel.getMinimumSize();
        JBInsets.addTo(size, listPopup.getContent().getInsets());
        listPopup.setMinimumSize(size);
        listPopup.showCenteredInCurrentWindow(project);
//        listPopup.showInBestPositionFor(dataContext);
    }


    public static String getInitialTextForNavigation(@Nullable Editor editor) {
        if (editor != null) {
            final String selectedText = editor.getSelectionModel().getSelectedText();
            if (selectedText != null && !selectedText.contains("\n")) {
                return selectedText;
            }
        }
        return null;
    }
}
